package com.wnxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wnxy.entity.Product;
import com.wnxy.mapper.ProductMapper;
import com.wnxy.service.ProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author 林夕
 * @since 2023-03-11 04:45:21
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    ProductMapper productMapper;

    @Override
    public List<Product> topN(Integer n) {
        QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.orderByDesc("created_time");
        productQueryWrapper.last("limit" + " " + n);
        List<Product> productList = productMapper.selectList(productQueryWrapper);
        return productList;
    }

    @Override
    public Page<Product> pageByType(Integer current, Integer pageSize, Integer typeId) {
        Page<Product> productPage = new Page<>(current,pageSize);

        QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.eq("product_type_id",typeId);
        productQueryWrapper.orderByDesc("created_time");
        Page<Product> productPage1 = productMapper.selectPage(productPage, productQueryWrapper);
        return productPage1;
    }
}
